package com.iot.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

public class YT {

    public static String SESSION_TOKEN = "SESSION_TOKEN";
    public static String USER_ID = "USER_ID";
    public static final String NO_MATCH_PATH = "*/(login).*";

    /**
     * @param decimalSource
     * @return String
     * @Description: 十进制转换成二进制 ()
     */
    public static String decimalToBinary(int decimalSource) {
//        BigInteger bi = new BigInteger(String.valueOf(decimalSource));	//转换成BigInteger类型
//        return bi.toString(2);	//参数2指定的是转化成X进制，默认10进制
        char[] chs = new char[8];
        for (int i = 0; i < 8; i++) {
            chs[8 - 1 - i] = (char) ((decimalSource >> i & 1) + '0');
        }
        return new String(chs);
    }

    public static String toBin(int num) {
        char[] chs = new char[8];
        for (int i = 0; i < 8; i++) {
            chs[8 - 1 - i] = (char) ((num >> i & 1) + '0');
        }
        return new String(chs);
    }

    /**
     * @param binarySource
     * @return int
     * @Description: 二进制转换成十进制
     */
    public static int binaryToDecimal(String binarySource) {
        BigInteger bi = new BigInteger(binarySource, 2);    //转换为BigInteger类型
        return Integer.parseInt(bi.toString());        //转换成十进制
    }

    public static String toUtf8 (String s) {

        try {
            return new String( s.getBytes("GBK") , "UTF-8");
//            return new String( s.getBytes("utf-8") , "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s;

    }
}
